const path = require('path');

function resolve(dir) {
    return path.join(__dirname, dir);
}

const CopyWebpackPlugin = require('copy-webpack-plugin');

const Version = require('crypto').createHash('md5').update(new Date().getTime().toString()).digest('hex').slice(0, 8);

const vueCliEnv = process.env;

const { VUE_APP_PUBLIC_PATH } = vueCliEnv;

module.exports = {
    publicPath: VUE_APP_PUBLIC_PATH,
    productionSourceMap: false, //是否生成map
    devServer: {
        disableHostCheck: true //禁用主机检查
    },
    pluginOptions: {
        'style-resources-loader': {
            preProcessor: 'less',
            patterns: [path.resolve(__dirname, 'src/assets/css/variable.less')]
        }
    },
    filenameHashing: true, // 在文件名中包含哈希值
    configureWebpack: {
        resolve: {
            alias: {
                '@': resolve('src'),
                '@http': resolve('src/utils/http.js'),
                '@api': resolve('src/utils/baseurl.js')
            }
        },
        plugins: [
            // new CopyWebpackPlugin([
            //     {
            //         from: './src/request_xml', //想要复制的文件夹
            //         to: 'request_xml' //复制在哪个文件夹
            //     }
            // ])
        ],
        output: {
            filename: `js/[name].${Version}.js`,
            chunkFilename: `js/[name].${Version}.js`
        },
        //该选项可以控制 webpack 如何通知「资源(asset)和入口起点超过指定文件限制」
        performance: {
            hints: 'warning', // 枚举
            maxAssetSize: 10485760, // 入口起点的最大体积（以字节为单位）
            maxEntrypointSize: 10485760, // 生成文件的最大体积（以字节为单位）
            assetFilter: function (assetFilename) {
                // 需要提示的文件类型
                return assetFilename.endsWith('.css') || assetFilename.endsWith('.js');
            }
        }
    },
    chainWebpack: (config) => {
        // 删除 prefetch 选项:  预加载
        config.plugins.delete('prefetch');
        if (process.env.NODE_ENV === 'production') {
            config.plugin('extract-css').tap(() => [
                {
                    filename: `css/[name].${Version}.css`,
                    chunkFilename: `css/[name].${Version}.css`
                }
            ]);
        }
    }
};
